﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        Word wo = new Word();
        Bitmap bm = new Bitmap(500, 900); // 初始化画布
        Plane plane = new Plane(200, 800); // 初始化飞机
        List<Obstacle> obstacles = new List<Obstacle>(); // 障碍物集合
        List<Bullet> bullets = new List<Bullet>(); // 子弹集合
        List<Weapon> weapons = new List<Weapon>(); // 武器集合
        Random rand = new Random();
        int score = 0; // 分数或飞行时间计数
        bool doubleBullet = false; // 是否开启双子弹模式
        int weaponCount = 0; // 用于记录拾取的武器数量
        private Image bkImage;
        public Form1()
        {
            InitializeComponent();
            this.KeyPreview = true; // 允许窗体捕获键盘事件
            bkImage = Image.FromFile("gif\\bk1.jpg");

            for (int i = 0; i < 20; i++)
            {
                var obstacle = new Obstacle(rand.Next(0, 450), rand.Next(-900, 0), rand.Next(20, 100), rand.Next(20, 50));
                obstacles.Add(obstacle);
            }
            // 初始化武器
            for (int i = 0; i < 2; i++)
            {
                var weapon = new Weapon(rand.Next(0, 450), rand.Next(-900, 0));
                weapons.Add(weapon);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Start(); // 开始游戏
            
        }
        private void timer1_Tick(object sender, EventArgs e)
        {

            // 更新画布到 PictureBox
            Draw();
            pictureBox1.Image = bm;
            pictureBox1.Refresh();
            //检查碰撞
            Check.Check_Ob(plane, obstacles);
            Check.Check_Bul(bullets, obstacles);
            
            // 10% 的概率生成新障碍物
            if (rand.Next(0, 100) <8) 
            {
                obstacles.Add(new Obstacle(rand.Next(0, 600), rand.Next(-900, 0), rand.Next(20, 100), rand.Next(20, 50)));
            }
            //
            if (rand.Next(0, 100) < 1)
            {
                var weapon = new Weapon(rand.Next(0, 450), rand.Next(-900, 0));
                weapons.Add(weapon);
            }
            // 判断飞机生命值
            if (plane.Life <= 0)
            {
                timer1.Stop();
                MessageBox.Show($"游戏结束，分数为: {score}");
            }
            // 检测障碍物是否超出画布
            foreach (var obstacle in obstacles)
            {
                if (obstacle.HasExitedCanvas())
                {
                    timer1.Stop();
                    MessageBox.Show($"游戏结束，分数为: {score}");
                    return;
                }
            }
            // 检查武器是否被拾取
            CheckWeaponPickup();
        }
        

   
        public void Draw()
        {
            Graphics g = Graphics.FromImage(bm); // 创建 Graphics 对象
            g.DrawImage(bkImage, 0, 0, bm.Width, bm.Height);


            //画飞机
            plane.Draw(g);

            //画障碍物
            foreach (var obstacle in obstacles)
            {
                obstacle.Draw(g);

            }

            //画子弹
            foreach (var bullet in bullets)
            {
                bullet.Draw(g);
                bullet.Move(); // 移动子弹
            }
            foreach (var weapon in weapons)
            {
                weapon.Draw(g);
            }

            // 显示分数和生命值
            g.DrawString($"Life: {plane.Life}", new Font("Arial", 16), Brushes.Black, new PointF(10, 10));
            g.DrawString($"Score: {score++}", new Font("Arial", 16), Brushes.Black, new PointF(10, 30));
        }
        private void CheckWeaponPickup()
        {
            foreach (var weapon in weapons)
            {
                if (weapon.GetRectangle().IntersectsWith(plane.GetRectangle()) && !weapon.IsCollected)
                {
                    weapon.IsCollected = true; // 设置武器已被拾取
                    weaponCount++; // 拾取武器计数+1

                    if (weaponCount == 3) // 第3个武器时
                    {
                        doubleBullet = true; // 开启双子弹模式
                    }
                    else
                    {
                        plane.Life++; // 其他武器增加生命值
                    }

                    weapons.Remove(weapon); // 移除已拾取的武器
                    break;
                }
            }
        }
        private void Form1_KeyPress(object sender, KeyPressEventArgs e)//键盘控制
        {
            plane.KeyPress(e.KeyChar);//飞机控制

            //空格键发射子弹
            if ((doubleBullet&& e.KeyChar == ' ')|| (doubleBullet && e.KeyChar =='f')) // 如果开启了双子弹模式
            {
                var bullet1 = new Bullet(plane.X +5, plane.Y); // 从飞机左侧发射子弹
                var bullet2 = new Bullet(plane.X + 35, plane.Y); // 从飞机右侧发射子弹
                bullets.Add(bullet1);
                bullets.Add(bullet2);
            }
            else if(e.KeyChar == ' ' || e.KeyChar == 'f')
            {
                var bullet = new Bullet(plane.X + 20, plane.Y); // 只从飞机右侧发射子弹
                bullets.Add(bullet);
            }
        }
    }
}
